package com.wing.product.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.UniqueIdGeneratorUtil;
import com.wing.product.dao.WarehouseProcessingReportDetailDao;
import com.wing.product.dao.WarehouseQualityTestingDao;
import com.wing.product.dao.WarehouseQualityTestingDetailDao;
import com.wing.product.model.entity.*;
import com.wing.product.model.request.WarehouseQualityTestingDetailForm;
import com.wing.product.model.request.WarehouseQualityTestingForm;
import com.wing.product.model.response.WarehouseQualityTestingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class WarehouseQualityTestingServiceImpl extends ServiceImpl<WarehouseQualityTestingDao, WarehouseQualityTesting> implements WarehouseQualityTestingService{

    @Autowired
    WarehouseQualityTestingDetailDao warehouseQualityTestingDetailDao;
    @Autowired
    WarehouseQualityTestingDetailService warehouseQualityTestingDetailService;
    @Autowired
    WarehouseProcessingReportService warehouseProcessingReportService;
    @Autowired
    WarehouseComputerPracticeService warehouseComputerPracticeService;
    @Autowired
    WarehouseProductionOrderService warehouseProductionOrderService;
    @Autowired
    WarehouseProductionSchedulingPlanService warehouseProductionSchedulingPlanService;
    @Autowired
    WarehouseProductionSchedulingPlanDetailService warehouseProductionSchedulingPlanDetailService;

    @Override
    public Page<WarehouseQualityTestingVO> pageList4app(Page page, Map<String, Object> params) {
        List<WarehouseQualityTestingVO> list = this.baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public WarehouseQualityTestingVO selectQualityTesting(String id){

        WarehouseQualityTestingVO warehouseQualityTestingVO = this.baseMapper.selectQualityTesting(id);
        LambdaQueryWrapper<WarehouseQualityTestingDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WarehouseQualityTestingDetail::getQualityTestingId,warehouseQualityTestingVO.getId())
                .eq(WarehouseQualityTestingDetail::getIsDeleted,0);
        List<WarehouseQualityTestingDetail> warehouseQualityTestingDetails = warehouseQualityTestingDetailDao.selectList(lambdaQueryWrapper);
        warehouseQualityTestingVO.setWarehouseQualityTestingDetailList(warehouseQualityTestingDetails);
        return warehouseQualityTestingVO;
    }

    @Override
    @Transactional
    public JsonResult saveWarehouseQualityTesting(WarehouseQualityTestingForm warehouseQualityTestingForm){

        if (ObjectUtil.isNotEmpty(warehouseQualityTestingForm)){
            //添加质检记录
            WarehouseQualityTesting warehouseQualityTesting = new WarehouseQualityTesting();
            CopyOptions copyOptions = CopyOptions.create()
                    .setEditable(WarehouseQualityTesting.class)
                    .setIgnoreError(true)
                    .setIgnoreNullValue(true);
            BeanUtil.copyProperties(warehouseQualityTestingForm, warehouseQualityTesting, copyOptions);
            warehouseQualityTesting.setCode(UniqueIdGeneratorUtil.generateUniqueId("ZJ", "yyyyMMddHHmmss", 1000, 9999));

            //查询报工记录
            LambdaQueryWrapper<WarehouseProcessingReport> lambdaQueryWrapperProcessingReport = new LambdaQueryWrapper<>();
            lambdaQueryWrapperProcessingReport.eq(WarehouseProcessingReport::getId,warehouseQualityTesting.getProcessingReportId());
            WarehouseProcessingReport warehouseProcessingReport = warehouseProcessingReportService.getOne(lambdaQueryWrapperProcessingReport);

            //查询上机记录
            LambdaQueryWrapper<WarehouseComputerPractice> lambdaQueryWrapperComputerPractice = new LambdaQueryWrapper<>();
            lambdaQueryWrapperComputerPractice.eq(WarehouseComputerPractice::getId,warehouseProcessingReport.getComputerPracticeId());
            WarehouseComputerPractice warehouseComputerPractice = warehouseComputerPracticeService.getOne(lambdaQueryWrapperComputerPractice);

            warehouseQualityTesting.setProductionPlanCode(warehouseComputerPractice.getProductionPlanCode());
            warehouseQualityTesting.setProductionPlanDetailCode(warehouseComputerPractice.getProductionSchedulingPlanDetailCode());
            warehouseQualityTesting.setStatus(1);

            this.baseMapper.insert(warehouseQualityTesting);

            //查询生产订单
            LambdaQueryWrapper<WarehouseProductionOrder> lambdaQueryWrapperProductionOrder = new LambdaQueryWrapper<>();
            lambdaQueryWrapperProductionOrder.eq(WarehouseProductionOrder::getId,warehouseQualityTestingForm.getProductionOrderId());
            WarehouseProductionOrder warehouseProductionOrder = warehouseProductionOrderService.getOne(lambdaQueryWrapperProductionOrder);

            //查询生产排产计划明细
            LambdaQueryWrapper<WarehouseProductionSchedulingPlanDetail> lambdaQueryWrapperProductionSchedulingPlanDetail = new LambdaQueryWrapper<>();
            lambdaQueryWrapperProductionSchedulingPlanDetail.eq(WarehouseProductionSchedulingPlanDetail::getId,warehouseComputerPractice.getProductionSchedulingPlanDetailId());
            WarehouseProductionSchedulingPlanDetail warehouseProductionSchedulingPlanDetail = warehouseProductionSchedulingPlanDetailService.getOne(lambdaQueryWrapperProductionSchedulingPlanDetail);

            //查询生产排产计划
            LambdaQueryWrapper<WarehouseProductionSchedulingPlan> lambdaQueryWrapperProductionSchedulingPlan = new LambdaQueryWrapper<>();
            lambdaQueryWrapperProductionSchedulingPlan.eq(WarehouseProductionSchedulingPlan::getId,warehouseProductionSchedulingPlanDetail.getWarehouseProductionSchedulingPlanId());
            WarehouseProductionSchedulingPlan warehouseProductionSchedulingPlan = warehouseProductionSchedulingPlanService.getOne(lambdaQueryWrapperProductionSchedulingPlan);




            Integer prodictionNumber = 0;
            Integer unProdictionNumber = 0;

            //添加质检记录明细
            if (CollectionUtils.isNotEmpty(warehouseQualityTestingForm.getWarehouseQualityTestingDetailList())){
                List<WarehouseQualityTestingDetail> warehouseQualityTestingDetailList = new ArrayList<>();
                for (WarehouseQualityTestingDetailForm qt : warehouseQualityTestingForm.getWarehouseQualityTestingDetailList()){
                    qt.setQualityTestingId(warehouseQualityTesting.getId());
                    WarehouseQualityTestingDetail warehouseQualityTestingDetail = new WarehouseQualityTestingDetail();
                    CopyOptions copyOptionsTwo = CopyOptions.create()
                            .setEditable(WarehouseQualityTestingDetail.class)
                            .setIgnoreError(true)
                            .setIgnoreNullValue(true);
                    BeanUtil.copyProperties(qt, warehouseQualityTestingDetail, copyOptionsTwo);
                    warehouseQualityTestingDetail.setStatus(1);
                    warehouseQualityTestingDetailList.add(warehouseQualityTestingDetail);
                    if (qt.getQualified() == 1){
                        prodictionNumber += qt.getInspectedQuantity();
                    }else {
                        unProdictionNumber += qt.getInspectedQuantity();
                    }
                }
                LambdaUpdateWrapper<WarehouseProcessingReport> lambdaQueryWrapper = new LambdaUpdateWrapper<>();
                lambdaQueryWrapper.eq(WarehouseProcessingReport::getId,warehouseQualityTesting.getProcessingReportId())
                        .set(WarehouseProcessingReport::getStatus,"2");
                warehouseProcessingReportService.update(lambdaQueryWrapper);
                warehouseQualityTestingDetailService.saveBatch(warehouseQualityTestingDetailList);

                //修改生产订单
                if (warehouseProductionOrder.getQualityTestingQualifiedNumber() != null){
                    warehouseProductionOrder.setQualityTestingQualifiedNumber(warehouseProductionOrder.getQualityTestingQualifiedNumber()+prodictionNumber);
                }else {
                    warehouseProductionOrder.setQualityTestingQualifiedNumber(prodictionNumber);
                }
                if (warehouseProductionOrder.getQualityTestingUnqualifiedNumber() != null){
                    warehouseProductionOrder.setQualityTestingUnqualifiedNumber(warehouseProductionOrder.getQualityTestingUnqualifiedNumber()+unProdictionNumber);
                }else {
                    warehouseProductionOrder.setQualityTestingUnqualifiedNumber(unProdictionNumber);
                }
                warehouseProductionOrderService.updateById(warehouseProductionOrder);

                //修改生产排产计划
                if (warehouseProductionSchedulingPlan.getQualityTestingQualifiedNumber()!= null){
                    warehouseProductionSchedulingPlan.setQualityTestingQualifiedNumber(warehouseProductionSchedulingPlan.getQualityTestingQualifiedNumber()+prodictionNumber);
                }else {
                    warehouseProductionSchedulingPlan.setQualityTestingQualifiedNumber(prodictionNumber);
                }
                if (warehouseProductionSchedulingPlan.getQualityTestingUnqualifiedNumber() != null){
                    warehouseProductionSchedulingPlan.setQualityTestingUnqualifiedNumber(warehouseProductionSchedulingPlan.getQualityTestingUnqualifiedNumber()+unProdictionNumber);
                }else {
                    warehouseProductionSchedulingPlan.setQualityTestingUnqualifiedNumber(unProdictionNumber);
                }
                warehouseProductionSchedulingPlanService.updateById(warehouseProductionSchedulingPlan);

                //修改生产排产计划明细
                if (warehouseProductionSchedulingPlanDetail.getQualityTestingQualifiedNumber()!= null){
                    warehouseProductionSchedulingPlanDetail.setQualityTestingQualifiedNumber(warehouseProductionSchedulingPlanDetail.getQualityTestingQualifiedNumber()+prodictionNumber);
                }else {
                    warehouseProductionSchedulingPlanDetail.setQualityTestingQualifiedNumber(prodictionNumber);
                }
                if (warehouseProductionSchedulingPlanDetail.getQualityTestingUnqualifiedNumber() != null){
                    warehouseProductionSchedulingPlanDetail.setQualityTestingUnqualifiedNumber(warehouseProductionSchedulingPlanDetail.getQualityTestingUnqualifiedNumber()+unProdictionNumber);
                }else {
                    warehouseProductionSchedulingPlanDetail.setQualityTestingUnqualifiedNumber(unProdictionNumber);
                }
                warehouseProductionSchedulingPlanDetailService.updateById(warehouseProductionSchedulingPlanDetail);

            }
            return JsonResult.success("添加成功");
        }else {
            return JsonResult.fail("添加失败");
        }
    }

    @Override
    @Transactional
    public  JsonResult updateeQualityTesting(WarehouseQualityTestingForm warehouseQualityTestingForm){

        if (ObjectUtil.isNotEmpty(warehouseQualityTestingForm)){
            WarehouseQualityTesting warehouseQualityTesting = new WarehouseQualityTesting();
            CopyOptions copyOptions = CopyOptions.create()
                    .setEditable(WarehouseQualityTesting.class)
                    .setIgnoreError(true)
                    .setIgnoreNullValue(true);
            BeanUtil.copyProperties(warehouseQualityTestingForm, warehouseQualityTesting, copyOptions);
            warehouseQualityTesting.setUpdateTime(LocalDateTime.now());
            this.baseMapper.updateById(warehouseQualityTesting);
            LambdaQueryWrapper<WarehouseQualityTestingDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(WarehouseQualityTestingDetail::getQualityTestingId,warehouseQualityTesting.getId())
                    .eq(WarehouseQualityTestingDetail::getIsDeleted,0);
            warehouseQualityTestingDetailDao.delete(lambdaQueryWrapper);
            if (CollectionUtils.isNotEmpty(warehouseQualityTestingForm.getWarehouseQualityTestingDetailList())){
                List<WarehouseQualityTestingDetail> warehouseQualityTestingDetailList = new ArrayList<>();
                for (WarehouseQualityTestingDetailForm qt : warehouseQualityTestingForm.getWarehouseQualityTestingDetailList()){
                    qt.setQualityTestingId(warehouseQualityTesting.getId());
                    WarehouseQualityTestingDetail warehouseQualityTestingDetail = new WarehouseQualityTestingDetail();
                    CopyOptions copyOptionsTwo = CopyOptions.create()
                            .setEditable(WarehouseQualityTestingDetail.class)
                            .setIgnoreError(true)
                            .setIgnoreNullValue(true);
                    BeanUtil.copyProperties(qt, warehouseQualityTestingDetail, copyOptionsTwo);
                    warehouseQualityTestingDetail.setUpdateTime(LocalDateTime.now());
                    warehouseQualityTestingDetailList.add(warehouseQualityTestingDetail);
                }
                warehouseQualityTestingDetailService.saveBatch(warehouseQualityTestingDetailList);
            }
            return JsonResult.success("修改成功");
        }else {
            return JsonResult.fail("修改失败");
        }
    }

    @Override
    public JsonResult deleteQualityTesting(String id){
        LambdaUpdateWrapper<WarehouseQualityTesting> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(WarehouseQualityTesting::getId,id)
                .set(WarehouseQualityTesting::getIsDeleted,1);
        this.update(lambdaUpdateWrapper);
        return JsonResult.success("操作成功");
    }
}
